package ti.ml.car;

import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import ti.ml.car.domain.Controller;
import ti.ml.car.domain.ISprite;

/**
 *
 * @author Sander
 */
public class Car implements ISprite {

    private final String carRight = "/images/CarRight.png";
    private final String carDown = "/images/CarDown.png";
    private final String carLeft = "/images/CarLeft.png";
    private final String carUp = "/images/CarUp.png";
    private ImageIcon iiRight, iiDown, iiLeft, iiUp;
    private Image image;
    private int dx, dy, x, y, pos;
    private int width, height;
    private final int[][] leveldata;
    private Controller c;

    public Car(Road road) {
        c = Controller.getIntance();
        leveldata = road.getScreenData();
        iiRight = new ImageIcon(this.getClass().getResource(carRight));
        iiLeft = new ImageIcon(this.getClass().getResource(carLeft));
        iiDown = new ImageIcon(this.getClass().getResource(carDown));
        iiUp = new ImageIcon(this.getClass().getResource(carUp));

        width = Road.getBlockSize();
        height = Road.getBlockSize();
        image = iiRight.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        pos = 40;
        x = 0;
        y = 2 * height;
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    public int getPos() {
        return pos;
    }

    public void resetPosition() {
        image = iiRight.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        x = 0;
        y = 2 * height;
    }

    @Override
    public Image getImage() {
        return image;
    }

    public void moveRight() {
        image = iiRight.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        c.appendTxtLog("Position in maze: " + pos);
        dx = Road.getBlockSize();

    }

    public void moveLeft() {
        image = iiLeft.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        c.appendTxtLog("Position in maze: " + pos);
        dx = -Road.getBlockSize();
    }

    public void moveUp() {
        image = iiUp.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        c.appendTxtLog("Position in maze: " + pos);
        dy = -Road.getBlockSize();
    }

    public void moveDown() {
        image = iiDown.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
        c.appendTxtLog("Position in maze: " + pos);
        dy = Road.getBlockSize();
    }

    @Override
    public void move() {
        int[] posOnScreen;

        if (x % Road.getBlockSize() == 0 || y % Road.getBlockSize() == 0) {
            if (y / Road.getBlockSize() == 0) {
            }

            pos = x / Road.getBlockSize()
                    + Road.getNrofblocks() * (int) (y / Road.getBlockSize());

            posOnScreen = leveldata[pos];
            leveldata[pos][4] = 2;

            if ((dx == -Road.getBlockSize() && dy == 0 && posOnScreen[3] == 1)
                    || (dx == Road.getBlockSize() && dy == 0 && posOnScreen[1] == 1)
                    || (dx == 0 && dy == -Road.getBlockSize() && posOnScreen[0] == 1)
                    || (dx == 0 && dy == Road.getBlockSize() && posOnScreen[2] == 1)) {

                dx = 0;
                dy = 0;
                c.appendTxtLog("Hitting wall");
            }

        }
        x += dx;
        y += dy;
        dx = 0;
        dy = 0;
    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
}